/*
 * GWTServiceImpl.java
 *
 * Created on 1 de noviembre de 2009, 07:10 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package py.fpuna.ii.electiva2.server;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.naming.InitialContext;
import py.fpuna.ii.electiva2.client.GWTService;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.NamingException;
import py.fpuna.ii.electiva2.dto.ClienteDTO;
import py.fpuna.ii.electiva2.interfaces.ClienteEAORemote;
import py.fpuna.ii.electiva2.client.model.ClienteModel;


/**
 *
 * @author Lauri
 */
public class GWTServiceImpl extends RemoteServiceServlet implements
        GWTService {

    
    public String guardarCliente(ClienteModel client) {
        try {
            Context ctx = new InitialContext();
            ClienteEAORemote clienteEaoR = (ClienteEAORemote) ctx.lookup("java:comp/env/ClienteEAOBean");
            
            ClienteDTO clientedto = new ClienteDTO();
            clientedto.setClieNombre(client.getClieNombre());
            clientedto.setClieApellido(client.getClieApellido());
            clientedto.setClieCi(client.getClieCi());
            clientedto.setClieDireccion(client.getClieDireccion());
            clientedto.setClieRuc(client.getClieRuc());

            clienteEaoR.create(clientedto);
            
            return "true";

        } catch (NamingException ex) {
            Logger.getLogger(GWTServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "false";
    }


    public List<ClienteModel> listarClientes(){
        List <ClienteModel> responseList = new ArrayList<ClienteModel>();
        
        try {

            Context ctx = new InitialContext();
            ClienteEAORemote clienteEaoR = (ClienteEAORemote) ctx.lookup("java:comp/env/ClienteEAOBean");

            //trae la lista de clientes de la BD
            List <ClienteDTO>clientList = new ArrayList<ClienteDTO>();

            clientList = clienteEaoR.findAll();

            for (ClienteDTO client : clientList){
                ClienteModel clientmod = new ClienteModel();
                clientmod.setClieNombre(client.getClieNombre());
                clientmod.setClieApellido(client.getClieApellido());
                clientmod.setClieCi(client.getClieCi());
                clientmod.setClieDireccion(client.getClieDireccion());
                clientmod.setClieRuc(client.getClieRuc());
                clientmod.setClieCodigo(client.getClieCodigo());

                responseList.add(clientmod);
            }

            return responseList;

        } catch (NamingException ex) {
            Logger.getLogger(GWTServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    /**
     * trae una lista de clientes segun una condicion
     * @param condicion
     * @return lista de clientes q machean la condicion
     */
    public List<ClienteModel> listarClientesEncontrados(String condicion) {
        List <ClienteModel> responseList = new ArrayList<ClienteModel>();
        
        try {

            Context ctx = new InitialContext();
            ClienteEAORemote clienteEaoR = (ClienteEAORemote) ctx.lookup("java:comp/env/ClienteEAOBean");

            //trae la lista de clientes de la BD
            List <ClienteDTO>clientList = new ArrayList<ClienteDTO>();

            clientList = clienteEaoR.getListaCliente(condicion);

            for (ClienteDTO client : clientList){
                ClienteModel clientmod = new ClienteModel();
                clientmod.setClieNombre(client.getClieNombre());
                clientmod.setClieApellido(client.getClieApellido());
                clientmod.setClieCi(client.getClieCi());
                clientmod.setClieDireccion(client.getClieDireccion());
                clientmod.setClieRuc(client.getClieRuc());
                clientmod.setClieCodigo(client.getClieCodigo());

                responseList.add(clientmod);
            }

            return responseList;

        } catch (NamingException ex) {
            Logger.getLogger(GWTServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }



    public String eliminarCliente(Integer idClient){
        String res = "";
        try {
            Context ctx = new InitialContext();
            ClienteEAORemote clienteEaoR = (ClienteEAORemote) ctx.lookup("java:comp/env/ClienteEAOBean");

            ClienteDTO clientdto = clienteEaoR.find(idClient);
            clienteEaoR.remove(clientdto);
            res = "eliminado";
        } catch (NamingException ex) {
            Logger.getLogger(GWTServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return res;
    }


    public String modificarCliente(Integer idClient, ClienteModel client){
        try {
            Context ctx = new InitialContext();
            ClienteEAORemote clienteEaoR = (ClienteEAORemote) ctx.lookup("java:comp/env/ClienteEAOBean");

            ClienteDTO clientedto = clienteEaoR.find(idClient);

            clientedto.setClieNombre(client.getClieNombre());
            clientedto.setClieApellido(client.getClieApellido());
            clientedto.setClieCi(client.getClieCi());
            clientedto.setClieDireccion(client.getClieDireccion());
            clientedto.setClieRuc(client.getClieRuc());

            clienteEaoR.edit(clientedto);

            return "modificado";

        } catch (NamingException ex) {
            Logger.getLogger(GWTServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "no modificado";
    }

}
